
cc.Class({
    extends: cc.Component,

    properties: {
        mahjong:cc.SpriteAtlas,
        card_me:cc.Prefab,
        card_Arc:cc.Prefab,
        card_Left:cc.Prefab,
        card_Right:cc.Prefab,
        outCard_Me:cc.Prefab,
        outCard_Arc:cc.Prefab,
        outCard_Left:cc.Prefab,
        outCard_Right:cc.Prefab,
        pgCard_Me:cc.Prefab,
        gangCard:cc.Prefab
    },
    initCardLeft(handCard,mo){
        cc.log('initCardLeft------>',handCard);
        let card = this.node.getChildByName('card_1');
        let handCards = card.getChildByName('handCard');
        let outCard = card.getChildByName('outCard');
        let pgCard = card.getChildByName('pgCard');
        pgCard.removeAllChildren();
        outCard.removeAllChildren();
        card.active = true;
        handCards.removeAllChildren();
        if(handCard){
            if(typeof handCard === 'number'){
                for(let i = 0; i < handCard ; i++){
                    let cardLeft = cc.instantiate(this.card_Left);
                    handCards.addChild(cardLeft);
                };
            };
        }
        let moCard = card.getChildByName('moCard');
        if(mo){
            if(moCard){
                moCard.position = cc.v2(-390,240);
                moCard.active = true;
            }else{
                let cardLeft = cc.instantiate(this.card_Left);
                card.addChild(cardLeft);
                cardLeft.position = cc.v2(-390,240);
            }
        }else if(moCard){
            moCard.destroy();
        }
    },
    initCardArc(handCard,mo){
        cc.log('initCardArc------>',handCard);
        let card = this.node.getChildByName('card_2');
        card.active = true;
        let handCards = card.getChildByName('handCard');
        handCards.removeAllChildren();
        let outCard = card.getChildByName('outCard');
        outCard.removeAllChildren();
        let pgCard = card.getChildByName('pgCard');
        pgCard.removeAllChildren();
        if(handCard){
            if(typeof handCard === 'number'){
                for(let i = 0; i < handCard ; i++){
                    let cardArc = cc.instantiate(this.card_Arc);
                    handCards.addChild(cardArc);
                };
            };
        }
        let moCard = card.getChildByName('moCard');
        if(mo){
            if(moCard){
                moCard.position = cc.v2(290,300);
                moCard.active = true;
            }else{
                let cardArc = cc.instantiate(this.card_Arc);
                card.addChild(cardArc);
                cardArc.position = cc.v2(290,300);
            }
        }else if(moCard){
            moCard.destroy();
        }
    },
    initMoCardLeft(){
        let card = this.node.getChildByName('card_1');
        let moCard = card.getChildByName('moCard');
        if(moCard){
            moCard.position = cc.v2(-390,240);
            moCard.active = true;
        }else{
            let cardLeft = cc.instantiate(this.card_Left);
            card.addChild(cardLeft);
            cardLeft.name = 'moCard';
            cardLeft.position = cc.v2(-390,240);
        }
    },
    initMoCardArc(){
        let card = this.node.getChildByName('card_2');
        let moCard = card.getChildByName('moCard');
        if(moCard){
            moCard.position = cc.v2(290,300);
            moCard.active = true;
        }else{
            let cardArc = cc.instantiate(this.card_Arc);
            card.addChild(cardArc);
            cardArc.name = 'moCard';
            cardArc.position = cc.v2(290,300);
        }
    },
    initMoCardRight(){
        let card = this.node.getChildByName('card_3');
        let moCard = card.getChildByName('moCard');
        if(moCard){
            moCard.position = cc.v2(390,-170);
            moCard.active = true;
        }else{
            let cardRight = cc.instantiate(this.card_Right);
            card.addChild(cardRight);
            cardRight.name = 'moCard';
            cardRight.position = cc.v2(390,-170);
        }
    },
    initCardRight(handCard,mo){
        cc.log('initCardRight------>',handCard);
        let card = this.node.getChildByName('card_3');
        card.active = true;
        let handCards = card.getChildByName('handCard');
        handCards.removeAllChildren();
        let outCard = card.getChildByName('outCard');
        outCard.removeAllChildren();
        let pgCard = card.getChildByName('pgCard');
        pgCard.removeAllChildren();
        if(handCard){
            if(typeof handCard === 'number'){
                for(let i = 0; i < handCard ; i++){
                    let cardRight = cc.instantiate(this.card_Right);
                    handCards.addChild(cardRight);
                };
            };
        }
        let moCard = card.getChildByName('moCard');
        if(mo){
            if(moCard){
                moCard.position = cc.v2(390,-170);
                moCard.active = true;
            }else{
                let cardRight = cc.instantiate(this.card_Right);
                card.addChild(cardRight);
                cardRight.name = 'moCard';
                cardRight.position = cc.v2(390,-170);
            }
        }else if(moCard){
            moCard.destroy();
        }
    },
    outCardLeft(data){
        let card = this.node.getChildByName('card_1');
        let handCards = card.getChildByName('handCard');
        let chilrdren = handCards.children;
        let chilrd = chilrdren[0];
        if(chilrd){
            chilrd.destroy();
        }
        let moCard = card.getChildByName('moCard');
        if(moCard){
            moCard.destroy();
        }
        let cardLeft = cc.instantiate(this.card_Left);
        handCards.addChild(cardLeft);
        let outCardLayout = card.getChildByName('outCard');
        let outCard = cc.instantiate(this.outCard_Left);
        outCardLayout.addChild(outCard);
        outCard.getComponent(cc.Sprite).spriteFrame = this.getCardSpritLeft(data);
        let childs = outCardLayout.children;
        for(let i=0;i<childs.length;i++){
            let childscript = childs[i].getComponent('outCard');
            if(i === childs.length -1){
                childscript.setPointer(true);
            }else{
                childscript.setPointer(false);
            }
        };
    },
    outCardArc(data){
        let card = this.node.getChildByName('card_2');
        let handCards = card.getChildByName('handCard');
        let chilrdren = handCards.children;
        let chilrd = chilrdren[0];
        if(chilrd){
            chilrd.destroy();
        }
        let moCard = card.getChildByName('moCard');
        if(moCard){
            moCard.destroy();
        }
        let cardArc = cc.instantiate(this.card_Arc);
        handCards.addChild(cardArc);
        let outCard = cc.instantiate(this.outCard_Arc);
        outCardLayout.addChild(outCard);
        outCard.getComponent(cc.Sprite).spriteFrame = this.getCardSpritMe(data); 
        let childs = outCardLayout.children;
        for(let i=0;i<childs.length;i++){
            let childscript = childs[i].getComponent('outCard');
            if(i === childs.length -1){
                childscript.setPointer(true);
            }else{
                childscript.setPointer(false);
            }
        }
    },
    outCardRight(data){
        let card = this.node.getChildByName('card_3');
        let handCards = card.getChildByName('handCard');
        let chilrdren = handCards.children;
        let chilrd = chilrdren[0];
        if(chilrd){
            chilrd.destroy();
        }
        let moCard = card.getChildByName('moCard');
        if(moCard){
            moCard.destroy();
        }
        let cardRight = cc.instantiate(this.card_Right);
        handCards.addChild(cardRight);
        let outCardLayout = card.getChildByName('outCard');
        let outCard = cc.instantiate(this.outCard_Right);
        outCardLayout.addChild(outCard);
        outCard.getComponent(cc.Sprite).spriteFrame = this.getCardSpritLeft(data);
        let childs = outCardLayout.children;
        for(let i=0;i<childs.length;i++){
            let childscript = childs[i].getComponent('outCard');
            if(i === childs.length -1){
                childscript.setPointer(true);
            }else{
                childscript.setPointer(false);
            }
        }
    },
    addOutCard(data){
        let card = this.node.getChildByName('card_0');
        let outCardLayout = card.getChildByName('outCard');
        let outCard = cc.instantiate(this.outCard_Me);
        outCardLayout.addChild(outCard);
        outCard.getComponent(cc.Sprite).spriteFrame = this.getCardSpritMe(data); 
        let childs = outCardLayout.children;
        for(let i=0;i<childs.length;i++){
            let childscript = childs[i].getComponent('outCard');
            if(i === childs.length -1){
                childscript.setPointer(true);
            }else{
                childscript.setPointer(false);
            }
        };
        let handcard = card.getChildByName('handCard');
        let handcardchildren = handcard.children;
        for(let i=0;i<handcardchildren.length;i++){
            let node = handcardchildren [i];
            let childscript = node.getComponent('cardMe');
            if(childscript._data == data){
                node.removeFromParent();
                node.destroy();
                break;
            };
        };
        this.arrangeOutCard();

    },
    start () {
        this.modata = -1;
        // this.node.on('outCard', (event) =>{
            
        // });
    },
    arrangeOutCard(){
        let card = this.node.getChildByName('card_0');
        let mocard = card.getChildByName('moCard');
        if(!mocard) return;
        let handCard = card.getChildByName('handCard');
        cc.log('handCard.children=====>',handCard.children);
        let moIndex = game.curGame.addMeHandCard();
        let newmocar = cc.instantiate(mocard);
        let type = this.getType(this.modata) - 1;
        let value = this.getValue(this.modata); 
        let name ='0_' + String(type) + String(value);
        if(type == 0){
            name ='0_'  + String(value);
        };
        if(this.modata == 51){
            name = '0_35';
        }else if(this.modata == 52){
            name = '0_36';
        }else if(this.modata == 53){
            name = '0_37';
        }
        newmocar.name = name;
        newmocar.y = 0 ;
        let cardScript = newmocar.getComponent('cardMe');
        cardScript.init(this.modata,this);
        newmocar.getComponent(cc.Sprite).spriteFrame = this.mahjong.getSpriteFrame(name);
        newmocar.opacity = 0;
        handCard.addChild(newmocar,moIndex);
        this.arrangeCardMe(newmocar);
    },
    arrangeCardMe(newmocar){
        let card = this.node.getChildByName('card_0');
        let mocard = card.getChildByName('moCard');
        let handCard = card.getChildByName('handCard');
        let chilrdren = handCard.children;
        let arr = [];  
        chilrdren.forEach((c,i)=>{
            let cs = c.getComponent('cardMe');
            arr.push({
                uuid:c.uuid,
                data:cs._data,
                x:c.x
            });
        });
        arr.sort((a,b)=>{
            if(this.getType(a.data) == this.getType(b.data)){
                return this.getValue(a.data) - this.getValue(b.data);
            }else{
                return this.getType(a.data) - this.getType(b.data);
            }
        });
        cc.log('arr=======>',arr);
        for(let i=0;i<chilrdren.length;i++){
            let node = chilrdren[i];
            for(let j = 0;j<arr.length;j++){
                if(node.uuid === arr[j].uuid){
                    node.zIndex = j;
                    let distance =  -36.5 - (chilrdren.length - j - 1) * 73;
                    let targetPos = cc.v2(distance,node.y);
                    let finished = cc.callFunc( ()=> {
                        node.position = targetPos;
                        let cardScript = node.getComponent('cardMe');
                        cardScript.upPos();
                    }, this);
                    let action = cc.sequence(cc.moveTo(0.5, targetPos), finished);
                    node.runAction(action);
                    if(mocard && newmocar && node.uuid == newmocar.uuid){
                        let wpos = handCard.convertToWorldSpaceAR(targetPos);
                        let cardPos = card.convertToNodeSpaceAR(wpos);
                        let p1x = mocard.x  - (Math.abs(mocard.x - cardPos.x) / 4);
                        let p2x = mocard.x  - (Math.abs(mocard.x - cardPos.x) / 4) * 3;
                        let bezier = [cc.v2(p1x, 0), cc.v2(p2x, 0), cardPos];
                        let finished2 = cc.callFunc( ()=> {
                            mocard.destroy();
                            newmocar.opacity = 255;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                        }, this);
                        let bezierTo = cc.sequence(cc.bezierTo(0.5, bezier), finished2);
                        mocard.runAction(bezierTo);
                    };
                    break;
                }
            }
        };
    },
    initCardMe(data,moCard){
        this.initCardMe(data);
        if(moCard != -1 ){
            this.initMoCard(moCard);
        }
    },
    getType(data){
        return Math.floor(data / 10);
    },
    getValue(data){
        return data % 10;
    },
    initMoCard(data){
        this.modata = data;
        let card = this.node.getChildByName('card_0');
        let cardMe = cc.instantiate(this.card_me);
        card.addChild(cardMe);
        cardMe.position = cc.v2(600,-300);
        let type = this.getType(data) - 1;
        let value = this.getValue(data); 
        let name ='0_' + String(type) + String(value);
        if(type == 0){
            name ='0_'  + String(value);
        };
        if(data == 51){
            name = '0_35';
        }else if(data == 52){
            name = '0_36';
        }else if(data == 53){
            name = '0_37';
        }
        cardMe.name = 'moCard';
        cardMe.getComponent(cc.Sprite).spriteFrame = this.mahjong.getSpriteFrame(name);
        let cardScript = cardMe.getComponent('cardMe');
        cardScript.init(data,this);
    },
    chi(dircation,cards,lcard){
        if(dircation === 0){
            let card = this.node.getChildByName('card_0');
            let handCard = card.getChildByName('handCard');
            let chilrdren = handCard.children;
            for(let j = 0;j < cards.length;j++){
                for(let i = chilrdren.length - 1;i >= 0;i--){
                    let cd = chilrdren[i];
                    let cds = cd.getComponent('cardMe');
                    if(cds._data == cards[j]){
                        cd.removeFromParent();
                        cd.destroy();
                        break;
                    };
                };
            };
            this.arrangeCardMe();
            //this.addpgCard(dircation,cards);
        }else {
            this.removHandCardCount(2,dircation);
        }
        cards.push(lcard);
        this.addchiCard(dircation,cards);
    },
    addchiCard(dircation,cards){
        console.log('cards====>',cards);
        let card = this.node.getChildByName('card_'+dircation);
        let pgCard = card.getChildByName('pgCard');
        let peng = cc.instantiate(this.pgCard_Me);
        pgCard.addChild(peng);
        let child = peng.children;
        for(let i=0;i<child.length;i++){
            let cd = child[i];
            let spriteFrame = this.getCardSpritMe(cards[i]);
            cd.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        };
    },
    minggang(dircation,cardgang){
        if(dircation === 0){
            let card = this.node.getChildByName('card_0');
            let handCard = card.getChildByName('handCard');
            let chilrdren = handCard.children;
            for(let j = 0;j < 3;j++){
                for(let i = chilrdren.length - 1;i>=0;i--){
                    let cd = chilrdren[i];
                    let cds = cd.getComponent('cardMe');
                    if(cds._data == cardpeng){
                        cd.removeFromParent();
                        cd.destroy();
                        break;
                    };
                };
            };
            this.arrangeCardMe();
        }else {
            this.removHandCardCount(3,dircation);
        }
        this.addmgCard(dircation,cardgang);
    },
    angang(dircation,cardgang){
        if(dircation === 0){
            let card = this.node.getChildByName('card_0');
            let handCard = card.getChildByName('handCard');
            let chilrdren = handCard.children;
            for(let j = 0;j < 3;j++){
                for(let i = chilrdren.length - 1;i>=0;i--){
                    let cd = chilrdren[i];
                    let cds = cd.getComponent('cardMe');
                    if(cds._data == cardpeng){
                        cd.removeFromParent();
                        cd.destroy();
                        break;
                    };
                };
            };
            let moCard = card.getChildByName('moCard');
            if(moCard){
                moCard.destroy();
            }
            this.arrangeCardMe();
        }else {
            this.removHandCardCount(3,dircation);
        }
        this.addmgCard(dircation,cardgang);
    },
    peng(dircation,cardpeng){
        if(dircation === 0){
            let card = this.node.getChildByName('card_0');
            let handCard = card.getChildByName('handCard');
            let chilrdren = handCard.children;
            for(let j = 0;j < 2;j++){
                for(let i = chilrdren.length - 1;i>=0;i--){
                    let cd = chilrdren[i];
                    let cds = cd.getComponent('cardMe');
                    if(cds._data == cardpeng){
                        cd.removeFromParent();
                        cd.destroy();
                        break;
                    };
                };
            };
            this.arrangeCardMe();
        }else{
            this.removHandCardCount(2,dircation);
        } ;
        this.addpgCard(dircation,cardpeng);
    },
    removHandCardCount(count,dircation){
        let card = this.node.getChildByName('card_'+dircation);
        let handCards = card.getChildByName('handCard');
        let chilrdren = handCards.children;
        for(let i  = 0;i<chilrdren.length ;i++){
            let cd = chilrdren[i];
            cd.destroy();
            if(i == count){
                break;
            }
        };
    },
    addpgCard(dircation,cardpeng){
        let card = this.node.getChildByName('card_'+dircation);
        let pgCard = card.getChildByName('pgCard');
        let peng = cc.instantiate(this.pgCard_Me);
        pgCard.addChild(peng);
        let child = peng.children;
        let spriteFrame = this.getCardSpritMe(cardpeng);
        for(let i=0;i<child.length;i++){
            let cd = child[i];
            cd.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        };
    },
    addmgCard(dircation,cardgang){
        let card = this.node.getChildByName('card_'+dircation);
        let pgCard = card.getChildByName('pgCard');
        let gang = cc.instantiate(this.gangCard);
        pgCard.addChild(gang);
        let child = gang.children;
        let spriteFrame = this.getCardSpritMe(cardgang);
        for(let i=0;i<child.length;i++){
            let cd = child[i];
            cd.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        };
    },
    initCardMe(data){
        data.sort((a,b)=>{
            if(this.getType(a) == this.getType(b)){
                return this.getValue(a) - this.getValue(b);
            }else{
                return this.getType(a) - this.getType(b);
            }
        });
        let card = this.node.getChildByName('card_0');
        let handCard = card.getChildByName('handCard');
        handCard.removeAllChildren();
        let outCard = card.getChildByName('outCard');
        outCard.removeAllChildren();
        let pgCard = card.getChildByName('pgCard');
        pgCard.removeAllChildren();
        for(let i = 0; i < data.length;i++){
            let type = this.getType(data[i]) - 1;
            let value = this.getValue(data[i]); 
            let name ='0_' + String(type) + String(value);
            if(type == 0){
                name ='0_'  + String(value);
            };
            if(data[i] == 51){
                name = '0_35';
            }else if(data[i] == 52){
                name = '0_36';
            }else if(data[i] == 53){
                name = '0_37';
            }
            let cardMe = cc.instantiate(this.card_me);
            handCard.addChild(cardMe);
            cardMe.x = -36.5 - (data.length - i - 1) * 73;
            cardMe.name = name;
            cardMe.zIndex = i ;
            cardMe.getComponent(cc.Sprite).spriteFrame = this.mahjong.getSpriteFrame(name);
            let cardScript = cardMe.getComponent('cardMe');
            cardScript.init(data[i],this);
        }
    },
    getCardSprit(data){
        let type = this.getType(data) - 1;
        let value = this.getValue(data); 
        let name ='0_' + String(type) + String(value);
        if(type == 0){
            name ='0_'  + String(value);
        };
        if(data == 51){
            name = '0_35';
        }else if(data == 52){
            name = '0_36';
        }else if(data == 53){
            name = '0_37';
        };
        return this.mahjong.getSpriteFrame(name);
    },
    getCardSpritLeft(data){
        let type = this.getType(data) - 1;
        let value = this.getValue(data); 
        let name ='3_' + String(type) + String(value);
        if(type == 0){
            name ='3_'  + String(value);
        };
        if(data == 51){
            name = '3_35';
        }else if(data == 52){
            name = '3_36';
        }else if(data == 53){
            name = '3_37';
        };
        return this.mahjong.getSpriteFrame(name);
    },
    getCardSpritMe(data){
        let type = this.getType(data) - 1;
        let value = this.getValue(data); 
        let name ='2_' + String(type) + String(value);
        if(type == 0){
            name ='2_'  + String(value);
        };
        if(data == 51){
            name = '2_35';
        }else if(data == 52){
            name = '2_36';
        }else if(data == 53){
            name = '2_37';
        };
        return this.mahjong.getSpriteFrame(name);
    }
    // update (dt) {},
});
